package com.mbc.common.manager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyDAO;
import com.mbc.common.dao.DBManager;
import com.mbc.common.dao.DBManagerFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.data.CurrencyDataList;
import com.mbc.common.data.CurrencyReferenceData;
import com.mbc.common.entities.Currency;
import com.mbc.common.entities.CurrencyRpt;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.utils.KeyGenerator;

public class CurrencyMgr extends AbstractMgr {
	
	private CurrencyDAO currencyDAO;
	private CurrencyDAO iaimsCurrencyDAO;
	private LogDAO logDAO;
	
	public CurrencyMgr()
	{}
	
	public CurrencyMgr(String sessionId)
	{
		this.sessionId = sessionId;
	}
	
	public void createCurrency(Currency currency)throws Exception{
		DBManager l_dbManager = DBManagerFactory.getDBManager(DBManager.LINKED_IAIMS);
		openConnection();
		try {
			beginTransaction();
			l_dbManager.beginTransaction();
			currency.setCurrencyId(KeyGenerator.generateKey(l_dbManager.getConnection()));
			
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			currencyDAO.create(currency);
			
			iaimsCurrencyDAO = CommonDAOFactory.getIAIMSCurrencyDAO(l_dbManager.getConnection());
			if(iaimsCurrencyDAO.findCurrencyByCode(currency.getCurrencyCode()) == null)
				iaimsCurrencyDAO.create(currency);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Currency");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(currency.getCurrencyId());
			l_logentity.setUserid(currency.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
			commintTransaction();
			l_dbManager.commitTransaction();
		} catch (SQLException e) {
			rollbackTransaction();
			l_dbManager.rollBackTransaction();
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		}
		finally{
			closeConnection();
			l_dbManager.closeConnection();
		}
	}
	
	public void updateCurrency(Currency currency)throws Exception{
		DBManager l_dbManager = DBManagerFactory.getDBManager(DBManager.LINKED_IAIMS);
		openConnection();
		try {
			beginTransaction();
			l_dbManager.beginTransaction();
			
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			currencyDAO.update(currency);
			
			iaimsCurrencyDAO = CommonDAOFactory.getIAIMSCurrencyDAO(l_dbManager.getConnection());
			iaimsCurrencyDAO.update(currency);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Currency");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(currency.getCurrencyId());
			l_logentity.setUserid(currency.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
			commintTransaction();
			l_dbManager.commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			l_dbManager.rollBackTransaction();
			throw e;
		}
		finally{
			l_dbManager.closeConnection();
			closeConnection();
		}
	}
	
	
	public void deleteCurrency(long currencyId,long userid)throws Exception{
		DBManager l_dbManager = DBManagerFactory.getDBManager(DBManager.LINKED_IAIMS);
		openConnection();
		try {
			beginTransaction();
			l_dbManager.beginTransaction();
			
			currencyDAO  = CommonDAOFactory.getCurrencyDAO(getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Currency");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(currencyId);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			currencyDAO.delete(currencyId);
			
			iaimsCurrencyDAO = CommonDAOFactory.getIAIMSCurrencyDAO(l_dbManager.getConnection());
			iaimsCurrencyDAO.delete(currencyId);
			
			commintTransaction();
			l_dbManager.commitTransaction();
		} catch (SQLException e) {
			rollbackTransaction();
			l_dbManager.rollBackTransaction();
			if(e.getErrorCode() == ForeignConflictException.FOREIGN_CONFLICT_ERROR_CODE)
				throw new ForeignConflictException(e);
			else
				throw e;
		}
		finally{
			closeConnection();
		}
	}
	
	public Currency findCurrency(long currencyId)throws Exception{
		openConnection();
		try {
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			return currencyDAO.find(currencyId);
		} catch (Exception e) {
			throw e;
		}
		finally{
			closeConnection();
		}
	}
	
	public ArrayList<Currency> getAll()throws Exception{
		openConnection();
		try {
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			return currencyDAO.findAll();
		} catch (Exception e) {
			throw e;
		}
		finally{
			closeConnection();
		}
	}
	
	public CurrencyDataList getCurrencyList(Currency cri) throws Exception{
		openConnection();
		try {
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			return currencyDAO.getList(cri);
		} catch (Exception e) {
			throw e;
		} finally{
			closeConnection();
		}
	}
	
	public ArrayList<CurrencyReferenceData> getCurrencyRefList()throws Exception{
		openConnection();
		try {
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			return currencyDAO.getCurrencyRefList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public JasperPrint preapreCurrencyReport(String fromCode, String toCode)throws Exception{
		openConnection();
		try {
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			JRResultSetDataSource l_jrDS = new JRResultSetDataSource(currencyDAO.prepareCurrencyData(fromCode, toCode));
			JasperReport l_jsRPT = (JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH + "reports/RMT_Currency.jasper");
			JasperPrint l_jsPrint = JasperFillManager.fillReport(l_jsRPT, null, l_jrDS);
			return l_jsPrint;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public JasperPrint prepareCurrencyReport(CurrencyRpt info) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			/*ResultSet l_rs = CommonDAOFactory.getCurrencyDAO(info,
					getConnection());*/
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(currencyDAO.prepareCurrencyReport(info));

			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
							+ "reports/CurrencyRpt.jasper");
			
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	/** MBC **/
	public Currency findCurrencyByCode(String code) throws Exception {
		openConnection();
		try {
			currencyDAO = CommonDAOFactory.getCurrencyDAO(dbManager.getConnection());
			return currencyDAO.findCurrencyByCode(code);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public static long getIAIMSCurrencyId(long currencyId, Connection localConnection, Connection linkedConnection)throws Exception{
		Currency l_localCurrency = CommonDAOFactory.getCurrencyDAO(localConnection).find(currencyId);
		
		CurrencyDAO l_iaimsCurrencyDAO = CommonDAOFactory.getCurrencyDAO(linkedConnection);
		Currency l_currency = l_iaimsCurrencyDAO.findCurrencyByCode(l_localCurrency.getCurrencyCode());
		if(l_currency == null){
			l_currency = l_localCurrency;
			l_currency.setCurrencyId(KeyGenerator.generateKey(linkedConnection));
			l_iaimsCurrencyDAO.create(l_currency);
		}
		return l_currency.getCurrencyId();
	}
}
